# coding: utf-8
"""
@python version : python3.10
@file name      : tasks.py
@date           : 2024/4/18 16:56
@author         : ziyang.yang@aliyun.com
@gitee          : https://gitee.com/ziyangyang1318
@blog           : www.yangziyang.top
@describe       : 
"""

from celery import shared_task
import time
from celery import Task
from django.core.mail import send_mail  # 基于django的邮箱发送
from django.conf import settings
import requests
from lxml import etree
from redis import Redis
from .models import Article

# 成功失败邮件告警
class SendEmailTask(Task):
    def on_success(self, retval, task_id, args, kwargs):
        info = f'任务成功-- 任务id是:{task_id} , 参数是:{args} , 执行成功 !'
        send_mail('celery任务监控成功告警', info, settings.EMAIL_HOST_USER, ["ziyang.yang@aliyun.com", ])

        print('------------成功')

    def on_failure(self, exc, task_id, args, kwargs, einfo):
        info = f'任务失败-- 任务id为:{task_id} , 参数为:{args} , 失败 ! 失败信息为: {exc}'
        send_mail('celery任务监控失败告警', info, settings.EMAIL_HOST_USER, ["ziyang.yang@aliyun.com", ])
        print('------------失败')

    def on_retry(self, exc, task_id, args, kwargs, einfo):
        print(f'任务id位：:{task_id} , 参数为:{args} , 重试了 !  错误信息为: {exc}')


@shared_task()
def add():
    time.sleep(1)
    print('结果是')
    return 10


@shared_task()
def send_email(mail):
    time.sleep(1)
    print(f'给{mail}发送邮件了')
    return '成功'


@shared_task(base=SendEmailTask, bind=True)  # 只要执行它成功或失败，都会给 ziyang.yang@aliyun.com发送邮件
def crawl_cnblogs(self):
    print('爬去cnblogs网站技术博客了')
    # 拿到redis链接
    conn = Redis(host='127.0.0.1', port=6379)
    headers = {
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'accept-language': 'zh-CN,zh;q=0.9',
        'cache-control': 'no-cache',
        # 'cookie': '_ga_2RM7FM6SS7=GS1.1.1686195308.1.0.1686195310.0.0.0; _ga_EWDMHPT0JG=GS1.1.1687944624.2.0.1687944624.0.0.0; _ga_4CQQXWHK3C=GS1.1.1688451806.1.0.1688451807.0.0.0; cto_bidid=HAUul19Md1JIRCUyQlh2WllxNGZLOUNaUmU0WjVwczM4WkF4V0VCaHo4TFd3WiUyQms1YWx0ODNHdjZ6ajFFT01IQ0FNeUJrdFNDdnFyOGpxb2dQa2xIYzBjRDRYVVNnOGlZMXlUQ3F4UDA2N2YwMGVUZmslM0Q; _ga_3XCB41CLNJ=GS1.1.1700124072.1.0.1700124082.0.0.0; _ga=GA1.1.1201588309.1676268794; _ga_7DSFGJNPL4=GS1.1.1700463535.3.0.1700463540.0.0.0; __51uvsct__JH1YrHz7W0txyG5w=1; __51vcke__JH1YrHz7W0txyG5w=5275dd43-5956-592f-bbb1-a664d9baf852; __51vuft__JH1YrHz7W0txyG5w=1705542920434; _cc_id=b2efeec07630b0f1f8ac09b5a70f6698; __gads=ID=0050038437674e14:T=1699413098:RT=1706251314:S=ALNI_MZnzikx_k_1z7SML1xeeiaaWCyyDA; __gpi=UID=00000c81ef14b771:T=1699413098:RT=1706251314:S=ALNI_MZXxw1h6ldYgZLraLnHWC-2I2MC7g; cto_bundle=bo9mm19zZ2VITUs3SXZsVjJWeWRWQUxCWW91byUyRlQlMkZtZVZRQWZ1OUdyOUlMNFpwZmhQeGlzNmxKNyUyRkJ3VmVUcFFqUjFtRU4yeUk2akFnc043VVR0ZDJOR3ZUdVM3MndPZVpaRDJCR2NoVVB3OXVVTDdac0dzN1ZyQ1ptSExObjkxSVlWZVgxTCUyRng2VUJYaXFjV1ZCcmhJU3hFQSUzRCUzRA; Hm_lvt_251bb25684a119a81c22d1489169733c=1711337772; _ga_3Q0DVSGN10=GS1.1.1711943462.7.0.1711943463.59.0.0; Hm_lvt_5fe1f53cf06d61b6db95af8d8d352ff0=1712727417; .AspNetCore.Antiforgery.b8-pDmTq1XM=CfDJ8C838EyK0EpKpQQcC9VwxBl0ErN1vjeYpoTM3w8fo9OJQL_lu2QZ774kslWSSblkjqY_Q6oxZjI2QE3QbyqrT0L_d9jSoR_qtKkiapXh3lXKCMtltKyiTc3_msSnrBYnAdHBTFjT24JsrHoYSVpMwR4; Hm_lvt_866c9be12d4a814454792b1fd0fed295=1712727417,1712801909,1713404394,1713490967; Hm_lpvt_866c9be12d4a814454792b1fd0fed295=1713496834; _ga_M95P3TTWJZ=GS1.1.1713496834.7.0.1713496834.0.0.0',
        'pragma': 'no-cache',
        'priority': 'u=0, i',
        'referer': 'https://www.baidu.com/link?url=q8IUzc63wX2cJOOqwuKvvVxaZZBYH4zkQ0UJKNxlzlRwMrgfc-MTHzFW24jJVTFf&wd=&eqid=bfe38dfa0024f68f000000046621e302',
        'sec-ch-ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'cross-site',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
    }

    response = requests.get('https://www.cnblogs.com/', headers=headers)

    tree = etree.HTML(response.text)
    article_list = tree.xpath('//*[@id="post_list"]/article')
    for article_tree in article_list:
        # 作者
        author = article_tree.xpath('./section/footer/a/span/text()')[0]
        # 文章标题
        title = article_tree.xpath('./section/div/a/text()')[0]
        # url
        content_url = article_tree.xpath('./section/div/a/@href')[0]
        # 摘要
        content_desc = ''.join(article_tree.xpath('./section/div/p/text()')).strip()
        print(f"""
        文章标题: {title}
        文章作者: {author}
        文章链接: {content_url}
        文章摘要: {content_desc}
        """)
        # 去重，不能有重复,如果重复了，就不存到数据库中了---》集合可以去重--》redis集合去重-->根据url去重
        # 只要redis的urls的这个集合中，有了这个地址，我们就不存了--》去重了
        res = conn.sadd('urls',content_url) # 每次爬取将url存到redis的集合中，有重复的 res就会返回0，就不会再存了
        if res:
            # 通过orm操作 写入数据库
            Article.objects.create(title=title, url=content_url, description=content_desc,author=author)
    return True
